package com.example.mqttxprovider.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mqttxprovider.callback.MqttConsumerCallBack;
import com.example.mqttxprovider.config.MqttConsumerConfig;
import com.example.mqttxprovider.service.MessageService;
import com.example.mqttxprovider.vo.ReturnVO;
import com.example.mqttxprovider.vo.dbVO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class MqttConsumerController {
    @Resource
    private MqttConsumerConfig client;
    @Resource
    private MessageService messageService;

    private MqttConsumerCallBack callBack = new MqttConsumerCallBack();

    @Value("${spring.mqtt.client.id}")
    private String clientId;

    @RequestMapping("/connect")
    public String connect(){
        client.connect();
        return clientId + "连接到服务器";
    }

    @RequestMapping("/disConnect")
    public String disConnect(){
        client.disConnect();
        return client + "与服务器断开连接";
    }

    @RequestMapping("/home/greenhouse/temperature")
    public ReturnVO temperature(){
        String topic = "home/greenhouse/temperature";
        String msg = callBack.getMesByTopic(topic);

        ReturnVO returnVO = new ReturnVO(msg.substring(3, 9), msg.substring(15, 20), null,null,null);
        return returnVO;
    }

    @RequestMapping(value = "/home/greenhouse/soil_moisture" ,method = RequestMethod.GET)
    public ReturnVO soil_moisture(){
        String topic = "home/greenhouse/soil_moisture";
        String msg = callBack.getMesByTopic(topic);
        ReturnVO returnVO = new ReturnVO();
        returnVO.setSoilMoisture(msg);

        return returnVO;
    }

    @RequestMapping("/home/greenhouse/humidity")
    public String humidity(){
        String topic = "home/greenhouse/humidity";
        String msg = callBack.getMesByTopic(topic);
        return msg;
    }

    @RequestMapping("/test/taspberry")
    public String taspberry(){
        String topic = "test/taspberry";
        String msg = callBack.getMesByTopic(topic);
        return msg;
    }

    @RequestMapping("/home/showTemperature")
    public Object showTemperature(HttpServletResponse response){
        List<dbVO> dbVOS = messageService.list(new QueryWrapper<dbVO>().eq("topic","home/greenhouse/temperature").orderByDesc("ts").last("limit 8"));

        List<ReturnVO> list = new ArrayList<>();
        for (dbVO dbVO : dbVOS) {
            ReturnVO returnVO = new ReturnVO();
            String msg = dbVO.getMsg();
            returnVO.setTemperature(msg.substring(3, 8));
            returnVO.setHumidity(msg.substring(15, 19));
            returnVO.setTs(dbVO.getTs());

            list.add(returnVO);
        }
        return list;
    }


    @RequestMapping("/home/showSoilMoisture")
    public Object showSoilMoisture(){
        List<dbVO> dbVOS = messageService.list(new QueryWrapper<dbVO>().eq("topic","home/greenhouse/soil_moisture").orderByDesc("ts").last("limit 8"));

        List<ReturnVO> list = new ArrayList<>();
        for (dbVO dbVO : dbVOS) {
            ReturnVO returnVO = new ReturnVO();
            String msg = dbVO.getMsg();
            returnVO.setSoilMoisture(msg.substring(5,9));
            returnVO.setPercentage(msg.substring(14,17));
            returnVO.setTs(dbVO.getTs());
            list.add(returnVO);
        }
        return list;
    }
}
